<template>
    <div class="pagination">
        <button :disabled="PageNo === 1" @click="nowPageNo -= 1">上一页</button>
        <button v-show="startEnd.start > 1"
                @click="nowPageNo = 1"
                :class="nowPageNo === 1 ? 'active':''"
        >1</button>

        <button v-show="startEnd.start > 2">···</button>

        <button v-for="item in pageShow"
                :key="item"
                @click="nowPageNo = item"
                :class="nowPageNo === item ? 'active':''"
        >{{item}}</button>

        <button v-show="startEnd.end < totalPages-1">···</button>
        <button v-show="startEnd.end < totalPages"
                @click="nowPageNo = totalPages"
                :class="nowPageNo === totalPages ? 'active':''"
        >{{totalPages}}</button>
        <button :disabled="continueNum === totalPages"
                @click="nowPageNo += 1"
        >下一页</button>

        <button style="margin-left: 30px">共 {{total}} 条</button>
    </div>
</template>

<script>
export default {
  name:"Pagination",
  props:["totalPages","PageNo","continueNum","changePageNo","total"],
  data(){
      return{
          nowPageNo : this.PageNo
      }
  },
  computed:{
      startEnd(){
          let start = 1;
          let end = 1;
          if(this.totalPages < this.continueNum){
              start = 1;
              end = this.totalPages
          }else {
              start = this.PageNo - Math.floor(this.continueNum/2)
              end = this.PageNo + Math.floor(this.continueNum/2)

              if(start < 1){
                  start = 1
                  end = this.continueNum
              }
              if(end > this.totalPages) {
                  end = this.totalPages
                  start = this.totalPages - this.continueNum + 1
              }
          }
          return {
              start,
              end
          }
      },
      pageShow(){
          const {start,end} = this.startEnd
          const arr = [];
          for (let i = start;i <= end ; i++){
              arr.push(i)
          }
          return arr
      }
  },
  watch:{
    nowPageNo:{
      handler(){
        this.$emit("changePageNo",this.nowPageNo)
      }
    }
  }
}
</script>

<style scoped lang="less">
.pagination {
    button {
        margin: 0 5px;
        background-color: #f4f4f5;
        color: #606266;
        outline: none;
        border-radius: 2px;
        padding: 0 4px;
        vertical-align: top;
        display: inline-block;
        font-size: 13px;
        min-width: 35.5px;
        height: 28px;
        line-height: 28px;
        cursor: pointer;
        box-sizing: border-box;
        text-align: center;
        border: 0;

        &[disabled] {
            color: #c0c4cc;
            cursor: not-allowed;
        }

        &.active {
            cursor: not-allowed;
            background-color: #409eff;
            color: #fff;
        }
    }
}
</style>